package esiag.pds.InformationManager;

import java.util.*;

import esiag.interfaces.IObserver;
import esiag.pds.beans.Information;
import esiag.pds.jms.Sender;
import esiag.psd.cacheManager.CacheManager;

public class InformationManager extends Thread implements IObserver  {

	private Sender sender;
	static int cp=1;
	//private MessageInOut m;
	private CacheManager cm;
	private InformationSource informationProvider;
	private boolean currentstate=true;
	public InformationManager(CacheManager cm,String queue,String url)  {
		// TODO Auto-generated constructor stub
		informationProvider=new InformationSource();
		this.cm=cm;
		sender=new Sender(cm,queue,url);
	}

	public void update(boolean reachable) {
		// TODO Auto-generated method stub
		if(!reachable){
			System.out.println(" ------------- Information put in cahe");
			addToCache(informationProvider.getInrmation());
			if(reachable!=currentstate)
			{
				currentstate=reachable;
				cm.ManagingCache();	
			}
		}
		else{
			if(cm.getGo()==true)
				cm.stop();			
			sender.send(informationProvider.getInrmation()); //et teste si le cache est vide et alterne
			}
		}
	
	void addToCache(Information inf ){
		cm.getCache().add(inf);
		for (int i = 0; i < cp ; i++) {
			cm.getCache().add(inf);
		}
		cp++;
	}
}


